package face;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import objects.*;
import dispatchers.*;
import java.sql.*;
import util.*;

public class Face extends HttpServlet{
	DBArticleDispatcher articleDispatcher = null;
	
	public void init(){
		DBManager dbManager = new DBManager("anagram","root","root");
		articleDispatcher = new DBArticleDispatcher(dbManager, "articles");
	}
	
	public void doPost(HttpServletRequest request, 
						HttpServletResponse response) throws ServletException, IOException{
		doGet(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		response.setContentType("text/html; charset=UTF-8");
		int articleID = 1;
		
		try{
			articleID = Integer.parseInt(request.getParameter("articleID"));
		}catch(NumberFormatException e){}
		
		addArticle(articleID, request);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("//index.jsp");
		dispatcher.forward(request, response);
	}
	
	private void addArticle(int articleID, HttpServletRequest request){
		Article article = articleDispatcher.getArticle(articleID);
		if (article==null)
			article = new Article(-1, "Ошибка!", "Запрашиваемая страница не найдена");
			
		request.setAttribute("article", article);		
	}
	
	
}